﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Telerik.WinControls.Enumerations;
using System.IO;
using System.Collections;
using log4net;
namespace QuanLyNhaSach
{
    public partial class frmDanhMucNhanVien : Form
    {
        //Tao Parttern Seleton
        static frmDanhMucNhanVien doituong = null;
        public static frmDanhMucNhanVien TaoDoiTuong()
        {
            if (doituong == null)
                doituong = new frmDanhMucNhanVien();
            return doituong;
        }

        public frmDanhMucNhanVien()
        {
            InitializeComponent();
        }

        public bool them;
        public bool sua;
        public bool xoa;
        public bool trangthai = false;
        int add = 0, update = 0, delete = 0;
        sv_QuanLyNhaSach.AdapterClient xuly = new sv_QuanLyNhaSach.AdapterClient();
        private ILog logger;
        ArrayList KiemTra_DanhSach = new ArrayList();

        private void frmDanhMucNhanVien_Load(object sender, EventArgs e)
        {
            logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            try
            {
                if (them == false)
                    bt_them.Enabled = false;
                else
                    bt_them.Enabled = true;

                if (sua == false)
                    bt_sua.Enabled = false;
                else
                    bt_sua.Enabled = true;

                if (xoa == false)
                    bt_xoa.Enabled = false;
                else
                    bt_xoa.Enabled = true;

                Load_NhanVien();

                this.gv_nhanvien.TableElement.BeginUpdate();
                this.gv_nhanvien.MasterTemplate.EnableFiltering = true;
                this.gv_nhanvien.MasterTemplate.AutoExpandGroups = true;
                this.gv_nhanvien.MasterTemplate.AllowAddNewRow = false;
                this.AddSummaries();
                this.gv_nhanvien.TableElement.EndUpdate();
                gv_nhanvien.EnableCustomFiltering = false;
                gv_nhanvien.ShowFilteringRow = false;
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }

        }

        private void Load_NhanVien()
        {
            try
            {
                DataSet ds = new DataSet();
                ds.ReadXml(new StringReader(xuly.LayDS_NhanVien()));
                if (ds.Tables.Count > 0)
                {
                    ds.Tables[0].Columns.Add("stt", typeof(int));
                    gv_nhanvien.DataSource = ds.Tables[0];
                    DataRow dr;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        dr = ds.Tables[0].NewRow();
                        dr["stt"] = i + 1;
                        ds.Tables[0].Rows[i]["stt"] = dr["stt"].ToString();
                        KiemTra_DanhSach.Add(ds.Tables[0].Rows[i]["MaNV"].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }

        }

        private void AddSummaries()
        {
            try
            {
                GridViewSummaryRowItem item1 = new GridViewSummaryRowItem();
                item1.Add(new GridViewSummaryItem("HoTenNV", "Tổng nhân viên: {0}", GridAggregateFunction.Count));
                this.gv_nhanvien.MasterTemplate.SummaryRowsTop.Add(item1);
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }

        }

        private void bt_them_Click(object sender, EventArgs e)
        {
            DataSet ds_tam = new DataSet();
            string mamoi = "";
            string maTam = "NV";
            try
            {
                ds_tam.ReadXml(new StringReader(xuly.selectqueryByText("select top 1 MaNV from NhanVien where MANV like '%" + maTam + "%'order by MANV desc")));
                if (ds_tam.Tables.Count == 0)
                    mamoi = maTam + "0001";
                else
                {
                    string matam = ds_tam.Tables[0].Rows[0][0].ToString();
                    string kituso = matam.Substring(2, 4).ToString();
                    int somoi = int.Parse(kituso) + 1;
                    mamoi = maTam + somoi.ToString("0000");
                }
                int dem = 0;
                int vitribatdau = gv_nhanvien.Rows.Count - 1;
                if (trangthai == false)
                {
                    if (them == true && xoa == true && sua == true)
                    {
                        bt_sua.Enabled = false;
                        bt_xoa.Enabled = false;
                    }
                    else
                    {
                        if (sua == false)
                            bt_sua.Enabled = false;
                        else
                            bt_sua.Enabled = false;

                        if (xoa == false)
                            bt_xoa.Enabled = false;
                        else
                            bt_xoa.Enabled = true;
                    }
                    bt_them.Image = global::QuanLyNhaSach.Properties.Resources.save;
                    bt_them.Text = "Lưu";
                    gv_nhanvien.AllowAddNewRow = true;
                    trangthai = true;

                    dem++;
                    gv_nhanvien.CurrentRow.Cells[1].Value = mamoi.ToString();
                    gv_nhanvien.Rows[gv_nhanvien.Rows.Count - 1].Cells["HoTenNV"].IsSelected = true;
                }
                else if (trangthai == true)
                {
                    for (int i = vitribatdau; i <= vitribatdau + dem; i++)
                    {
                        if (!KiemTra_DanhSach.Contains(gv_nhanvien.Rows[i].Cells["MaNV"].Value.ToString()))
                        {
                            string manv = mamoi;
                            string tennv = gv_nhanvien.CurrentRow.Cells["HoTenNV"].Value.ToString().Trim();
                            string sdt = gv_nhanvien.CurrentRow.Cells["SoDTNV"].Value.ToString().Trim();
                            DateTime ngaysinh = DateTime.Parse(gv_nhanvien.CurrentRow.Cells["NgaySinhNV"].Value.ToString().Trim());
                            string diachi = gv_nhanvien.CurrentRow.Cells["DiaChiNV"].Value.ToString().Trim();
                            string cmnd = gv_nhanvien.CurrentRow.Cells["CMNDNV"].Value.ToString().Trim();
                            string chucvu = gv_nhanvien.CurrentRow.Cells["ChucVu"].Value.ToString().Trim();
                            if (!xuly.InsertNV(manv, tennv, sdt, ngaysinh, diachi, cmnd, chucvu))
                            {
                                MessageBox.Show("Thêm thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                return;
                            }
                        }
                    }
                    add++;
                    MessageBox.Show("Thêm thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Load_NhanVien();
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }

        private void bt_sua_Click(object sender, EventArgs e)
        {
            try
            {
                if (trangthai == false)
                {
                    if (them == true && xoa == true && sua == true)
                    {
                        bt_them.Enabled = false;
                        bt_xoa.Enabled = false;
                    }
                    else
                    {
                        if (them == false)
                            bt_them.Enabled = false;
                        else
                            bt_them.Enabled = false;

                        if (xoa == false)
                            bt_xoa.Enabled = false;
                        else
                            bt_xoa.Enabled = true;
                    }
                    //giao dien
                    bt_sua.Image = global::QuanLyNhaSach.Properties.Resources.save;
                    bt_sua.Text = "Lưu";
                    trangthai = true;
                    gv_nhanvien.ReadOnly = false;

                }
                else if (trangthai == true)
                {
                    if (them == true && xoa == true && sua == true)
                    {
                        bt_them.Enabled = true;
                        bt_xoa.Enabled = true;
                    }
                    else
                    {
                        if (them == false)
                            bt_them.Enabled = false;
                        else
                            bt_them.Enabled = true;

                        if (xoa == false)
                            bt_xoa.Enabled = false;
                        else
                            bt_xoa.Enabled = true;
                    }

                    //errorProvider1.Clear();
                    //Sua Thong Tin Khach Hang
                    string manv = gv_nhanvien.CurrentRow.Cells["MaNV"].Value.ToString().Trim();
                    string tennv = gv_nhanvien.CurrentRow.Cells["HoTenNV"].Value.ToString().Trim();
                    string sdt = gv_nhanvien.CurrentRow.Cells["SoDTNV"].Value.ToString().Trim();
                    DateTime ngaysinh = DateTime.Parse(gv_nhanvien.CurrentRow.Cells["NgaySinhNV"].Value.ToString().Trim());
                    string diachi = gv_nhanvien.CurrentRow.Cells["DiaChiNV"].Value.ToString().Trim();
                    string cmnd = gv_nhanvien.CurrentRow.Cells["CMNDNV"].Value.ToString().Trim();
                    string chucvu = gv_nhanvien.CurrentRow.Cells["ChucVu"].Value.ToString().Trim();
                    if (!xuly.UpdateNV(manv, tennv, sdt, ngaysinh, diachi, cmnd, chucvu))
                    {
                        MessageBox.Show("Sửa thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    update++;
                    MessageBox.Show("Sửa thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Load_NhanVien();

                    //giao diện
                    bt_sua.Image = global::QuanLyNhaSach.Properties.Resources.repair_new;
                    bt_sua.Text = "Sửa";
                    trangthai = false;
                    gv_nhanvien.ReadOnly = true;
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }

        }

        private void bt_xoa_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Bạn thực sự muốn xoá nhân viên này", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string manv = gv_nhanvien.CurrentRow.Cells["MaNV"].Value.ToString();
                    if (!xuly.DeleteNV(manv))
                    {
                        MessageBox.Show("Xoá thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else
                        MessageBox.Show("Xoá thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Load_NhanVien();
                    delete++;
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }

                
        }

        private void bt_ngung_Click(object sender, EventArgs e)
        {
            try
            {
                errorProvider1.Clear();

                if (them == false)
                {
                    bt_them.Enabled = false;
                    bt_them.Image = global::QuanLyNhaSach.Properties.Resources._new;
                    bt_them.Text = "Thêm";
                    gv_nhanvien.AllowAddNewRow = false;
                }
                else
                {
                    bt_them.Enabled = true;
                    bt_them.Image = global::QuanLyNhaSach.Properties.Resources._new;
                    bt_them.Text = "Thêm";
                    gv_nhanvien.AllowAddNewRow = false;
                }

                if (sua == false)
                {
                    bt_sua.Enabled = false;
                    bt_sua.Image = global::QuanLyNhaSach.Properties.Resources.repair_new;
                    bt_sua.Text = "Sửa";
                    gv_nhanvien.AllowEditRow = false;
                }
                else
                {
                    bt_sua.Image = global::QuanLyNhaSach.Properties.Resources.repair_new;
                    bt_sua.Text = "Sửa";
                    bt_sua.Enabled = true;
                    gv_nhanvien.AllowEditRow = false;
                }

                if (xoa == false)
                    bt_xoa.Enabled = false;
                else
                    bt_xoa.Enabled = true;

                trangthai = false;
                Load_NhanVien();
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }

        }

        private void bt_excel_Click(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }

        private void bt_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bt_tim_Click(object sender, EventArgs e)
        {
            try
            {
                if (bt_tim.Text == "Tìm kiếm")
                {
                    bt_tim.Text = "Trở lại";
                    bt_tim.Image = global::QuanLyNhaSach.Properties.Resources.go_back_icon;
                    gv_nhanvien.EnableFiltering = true;
                    gv_nhanvien.ShowFilteringRow = true;
                }
                else if (bt_tim.Text == "Trở lại")
                {
                    bt_tim.Text = "Tìm kiếm";
                    bt_tim.Image = global::QuanLyNhaSach.Properties.Resources.loc;
                    gv_nhanvien.EnableFiltering = false;
                    gv_nhanvien.ShowFilteringRow = false;
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }

        }

        private void bt_xemin_Click(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }

        private void bt_lamtuoi_Click(object sender, EventArgs e)
        {
            try
            {
                Load_NhanVien();
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
            
        }
    }
}
